package controller;

import com.google.gson.Gson;
import dao.EquipmentDaoImpl;
import domain.Equipment;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.HashMap;


/**
 * 简介说明: 设备servlet
 *
 * @author: Half_Q_0115
 * @date: 2024/09/17 21:00:32
 * @version: 1.0
 */

@WebServlet("/equipmentServlet")
public class EquipmentServlet extends HttpServlet {
    private EquipmentDaoImpl equipmentDao = new EquipmentDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method = req.getParameter("method");
        if("getAllEquipment".equals(method)){
            this.getAllEquipment(req,resp);
        }else if("getEquipmentById".equals(method)){
            this.getEquipmentById(req,resp);
        }else if ("searchEquipment".equals(method)) {
            this.searchEquipment(req, resp);
        }else if ("addEquipment".equals(method)) {
            this.addEquipment(req, resp);
        }else if("modifyEquipment".equals(method)){
            this.modifyEquipment(req,resp);
        }else if("deleteEquipmentById".equals(method)){
            this.deleteEquipmentById(req,resp);
        }else if("getMyEquipment".equals(method)){
            this.getMyEquipment(req, resp);
        }
        else{
            System.out.println("请求地址错误");
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }


    private void addEquipment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String data = req.getParameter("data");
        int college_id = (Integer)req.getSession().getAttribute("college_id");
        String staff_id = (String)req.getSession().getAttribute("id");
        Gson gson = new Gson();
        Equipment equipment = gson.fromJson(data, Equipment.class);
        equipment.setCollege_id(college_id);
        equipment.setStaff_id(staff_id);
        int ret = equipmentDao.addEquipment(equipment);
        PrintWriter writer = resp.getWriter();
        if(ret>0){
            writer.print(1);
        }else {
            writer.print(0);
        }
        writer.close();
    }

    private void getAllEquipment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Equipment> list = equipmentDao.findAllEquipment();
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getMyEquipment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String staff_id = (String)req.getSession().getAttribute("id");
        List<Equipment> list = equipmentDao.findMyEquipment(staff_id);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getEquipmentById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Equipment equipment = equipmentDao.findEquipmentById(Integer.parseInt(id));
        HashMap<String, Object> map = new LinkedHashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", equipment);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void searchEquipment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String college_id = req.getParameter("college_id");
        String staff_id = req.getParameter("staff_id");

        List<Equipment> list = equipmentDao.searchEquipment(id, name, type, college_id, staff_id);

        HashMap<String, Object> map = new LinkedHashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);

        Gson gson = new Gson();
        String json = gson.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void modifyEquipment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String data = req.getParameter("data");
        Gson gson = new Gson();
        Equipment equipment = gson.fromJson(data, Equipment.class);

        int ret = equipmentDao.modifyEquipment(equipment);
        PrintWriter writer = resp.getWriter();
        if (ret > 0) {
            writer.print(1);
        } else {
            writer.print(0);
        }
        writer.close();
    }

    private void deleteEquipmentById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        int result = equipmentDao.deleteEquipmentById(id);
        PrintWriter writer = resp.getWriter();
        if (result > 0) {
            writer.print(1);
        }else {
            writer.print(0);
        }
        writer.close();
    }

}
